


// var schoolActiveArr = [{"id":"a001","schoolName":"成都第一中学","schoolNo":"sc001","principal":"肖明","startTime":"2020-11-29","title":"元旦节日","tNo":"t001","content":"元旦节日","activeTime":"2021-1-1","activeTel":"13789011267","State":"待审"},
//     {"id":"a002","schoolName":"成都第二中学","schoolNo":"sc002","principal":"谭微","startTime":"2020-12-30","title":"元旦节日","tNo":"t002","content":"元旦节日","activeTime":"2021-1-1","activeTel":"13689012337","State":"待审"},
//     {"id":"a003","schoolName":"成都七中","schoolNo":"sc003","principal":"李天","startTime":"2020-12-03","title":"元旦节日","tNo":"t003","content":"元旦节日","activeTime":"2021-1-1","activeTel":"15289013227","State":"通过"},
//     {"id":"a004","schoolName":"成都树德中学","schoolNo":"sc004","principal":"张翠花","startTime":"2020-12-05","title":"元旦节日","tNo":"t004","content":"元旦节日","activeTime":"2021-1-1","activeTel":"16789067867","State":"通过"},
//     {"id":"a005","schoolName":"绵阳中学","schoolNo":"sc005","principal":"梁星","startTime":"2020-12-07","title":"元旦节日","tNo":"t005","content":"元旦节日","activeTime":"2021-1-1","activeTel":"18480081287","State":"接洽中"},
//     {"id":"a006","schoolName":"南山中学","schoolNo":"sc006","principal":"李亚龙","startTime":"2020-12-12","title":"元旦节日","tNo":"t006","content":"元旦节日","activeTime":"2021-1-1","activeTel":"19089011007","State":"待审"},
//     {"id":"a007","schoolName":"绵阳实验中学","schoolNo":"sc007","principal":"王雪","startTime":"2020-12-14","title":"元旦节日","tNo":"t007","content":"元旦节日","activeTime":"2021-1-1","activeTel":"16989011097","State":"接洽中"},
//     {"id":"a008","schoolName":"绵阳科学城一中","schoolNo":"sc008","principal":"龚梓","startTime":"2020-12-26","title":"元旦节日","tNo":"t008","content":"元旦节日","activeTime":"2021-1-1","activeTel":"14789015697","State":"通过"},
// ];
$(function () {


    var totalPage;//总页码
    var currentPage=1;//当前页
    var pageSize=6;
    var  operid=-1;
    // startFuc();

    // function startFuc(){

        bindPage();
       // search();
        addPageEvent();
        addDelEvent();
        displayActiveList();
        checkAll();//全选事件
        checkItemAll();
        add();
        upDateActive();
        selectActive();
        // check();
        // length();
        // inputText();
    // }


    //查找活动
    // function search() {
    //     // $('.schoolActive').on('click','.btnSearch',function () {
    //     //     console.log("lll");
    //     //     var val=$('.activeSearch').val();
    //     //     $('#tbActive td').hide().filter(':content('+val+')').show();
    //     // })
    //
    //     $(".icon-sousuo").click(function () {
    //         var val=$('.activeSearch').val();
    //          $('#tbActive td').hide().filter(':content('+val+')').show();
    //     });
    // }



//全选
function checkAll() {
    $("#checkAll").click(function () {
        var checked=this.checked
        $("input[name='checkItem']").each(function (index,item) {
            item.checked=checked;

        })
    })
}

//全选的列表
function checkItemAll() {
    $("#tbActive ").on('click','input[name=\'checkItem\']',function () {
        var isAll=true;
        $("input[name='checkItem']").each(function (index,item) {
            if(!item.checked){
                isAll=false;
            }
        })
        if(isAll){
            $("#checkAll")[0].checked=true;
        }else {
            $("#checkAll")[0].checked=false;
        }
    })

}

//下一页
function addPageEvent() {
    $("#activePage").on("click",".btn",function () {
        currentPage=Number($(this).text());
        displayActiveList();
    })
}

//生成页码
function bindPage() {
    if (sessionStorage.getItem("schoolActiveArr")){
        var arr = sessionStorage.getItem("schoolActiveArr");
        schoolActiveArr = JSON.parse(arr);
    }
    var totalSize=schoolActiveArr.length;
    totalPage=Math.ceil(totalSize/pageSize);
    if(totalPage<currentPage){
        currentPage=totalPage;
    }

    $("#activePage").html("");
    var str = "";
    for (var i=1;i<=totalPage;i++){
        if(i==currentPage){
            str += `<button type="button" class="de_pageBtActive">${i}</button>`;
        }else {
            str += `<button type="button">${i}</button>`;
        }
        // $("#activePage").append(`<button class="btn btn-default">${i}</button>>`)
    }
    $("#activePage").html(str);
}

    pageBtClick();

//分页按钮点击功能实现函数
    function pageBtClick() {
        $(".de_pageBt").on("click","button",function () {
            // console.log($(this).text());
            $(".de_pageBt button").each(function () {
                $(this).attr("class","");        //清空所有按钮的点击class属性
            });
            $(this).addClass("de_pageBtActive");
            currentPage = parseInt($(this).text());
            // getTable();
            displayActiveList();
        });
    }

//数据条数
// function length() {
//     var txt=schoolActiveArr.length;
//     $('.length').text(txt);
// }

//下拉列表查询信息
    function selectActive() {
        $("#si").change(function () {
            var newArr = [];
            $("#tbActive").html("");
            var cityName = $("#si").val();
            if (sessionStorage.getItem("schoolActiveArr")){
                var arr = sessionStorage.getItem("schoolActiveArr");
                schoolActiveArr = JSON.parse(arr);
            }
            if(cityName == "cd"){
                for(var i=0;i<schoolActiveArr.length;i++){
                    if(schoolActiveArr[i].schoolName.indexOf("成都")!=-1){
                        newArr.push(schoolActiveArr[i]);
                    }
                }

                for(var i=0;i<newArr.length;i++){
                    $("#tbActive").append(`<tr>
                        <td index="${i}"><input type="checkbox" name=\"checkItem\"></td>
                        <td index="${i}">${newArr[i].id}</td>
                        <td index="${i}">${newArr[i].startTime}</td>
                        <td index="${i}">${newArr[i].schoolName}</td>
                        <td index="${i}">${newArr[i].title}</td>
                        <td index="${i}">${newArr[i].content}</td>
                        <td index="${i}">${newArr[i].activeTime}</td>
                        <td index="${i}">${newArr[i].principal}</td>
                        <td index="${i}">${newArr[i].activeTel}</td>
                        <td index="${i}">${newArr[i].State}</td>
                        <td index="${i}"><span class="del" onclick="del()" data-id="${newArr[i].id}">删除</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="upDate"  index="${i}" data-id="${newArr[i].id}">修改</span></td>
                    </tr>`)
                }
            }else if(cityName == "my"){
                if (sessionStorage.getItem("schoolActiveArr")){
                    var arr = sessionStorage.getItem("schoolActiveArr");
                    schoolActiveArr = JSON.parse(arr);
                }
                for(var i=0;i<schoolActiveArr.length;i++){
                    if(schoolActiveArr[i].schoolName.indexOf("绵阳")!=-1){
                        newArr.push(schoolActiveArr[i]);
                    }
                }
                for(var i=0;i<newArr.length;i++){
                    $("#tbActive").append(`<tr>
                        <td index="${i}"><input type="checkbox" name=\"checkItem\"></td>
                        <td index="${i}">${newArr[i].id}</td>
                        <td index="${i}">${newArr[i].startTime}</td>
                        <td index="${i}">${newArr[i].schoolName}</td>
                        <td index="${i}">${newArr[i].title}</td>
                        <td index="${i}">${newArr[i].content}</td>
                        <td index="${i}">${newArr[i].activeTime}</td>
                        <td index="${i}">${newArr[i].principal}</td>
                        <td index="${i}">${newArr[i].activeTel}</td>
                        <td index="${i}">${newArr[i].State}</td>
                        <td index="${i}"><span class="del" onclick="del()" data-id="${newArr[i].id}">删除</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="upDate" index="${i}" data-id="${newArr[i].id}">修改</span></td>
                    </tr>`)
                }
            }else if(cityName == "all"){
                $("#tbActive").html("");
                $("#checkAll")[0].checked=false;
                var start=(currentPage-1)*pageSize;
                var end=start+pageSize;
                if (sessionStorage.getItem("schoolActiveArr")){
                    var arr = sessionStorage.getItem("schoolActiveArr");
                    schoolActiveArr = JSON.parse(arr);
                }
                for(var i=start;i<end&&schoolActiveArr.length;i++){
                    $("#tbActive").append(`<tr>
                        <td index="${i}"><input type="checkbox" name=\"checkItem\"></td>
                        <td index="${i}">${schoolActiveArr[i].id}</td>
                        <td index="${i}">${schoolActiveArr[i].startTime}</td>
                        <td index="${i}">${schoolActiveArr[i].schoolName}</td>
                        <td index="${i}">${schoolActiveArr[i].title}</td>
                        <td index="${i}">${schoolActiveArr[i].content}</td>
                        <td index="${i}">${schoolActiveArr[i].activeTime}</td>
                        <td index="${i}">${schoolActiveArr[i].principal}</td>
                        <td index="${i}"> ${schoolActiveArr[i].activeTel}</td>
                        <td index="${i}">${schoolActiveArr[i].State}</td>
                        <td index="${i}"><span class="del" onclick="del()" data-id="${schoolActiveArr[i].id}">删除</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="upDate" index="${i}" data-id="${schoolActiveArr[i].id}">修改</span></td>
                    </tr>`)
                }
            }
        });


    }


//添加活动
function add() {
    $("#btnAddActive").click(function () {
        operid=-1;
        $("#addActive").modal('show');
    })

    $("#btnAddSure").click(function () {
        //添加数据，获取表单数据
        // var Si=$("#addSi").val();
        if (sessionStorage.getItem("schoolActiveArr")){
            var arr = sessionStorage.getItem("schoolActiveArr");
            schoolActiveArr = JSON.parse(arr);
        }
        var id=$('#addId').val();
        var startTime=$("#addStartTime").val();
        var schoolName=$("#addSchool").val();
        var title=$("#addTitle").val();
        var content=$("#addContent").val();
        var activeTime=$("#addActiveTime").val();
        var principal=$("#addPrincipal").val();
        var activeTel=$("#addActiveTel").val();
        var State=$("#addState").val();
        //var id = schoolActiveArr[schoolActiveArr.length-1].id;
        //自动获取id，
        //id = parseInt(id.substring(1))+1;
        // console.log(parseInt(id.substring(1)))
        // console.log("活动负责人"+principal);
        var obj={"id":id,"startTime":startTime,"schoolName":schoolName,"title":title,"content":content,"activeTime":activeTime,"principal":principal,"activeTel":activeTel,"State":State};
        if(operid==-1){
            schoolActiveArr.push(obj);
            setEditInfo(`添加了${obj.schoolName}的活动`);
        }else {
             schoolActiveArr.splice(operid,1,obj);
            setEditInfo(`修改了${schoolActiveArr[operid].schoolName}的活动`);
        }
        var str = JSON.stringify(schoolActiveArr);
        sessionStorage.setItem("schoolActiveArr",str);
        displayActiveList();
        bindPage();
        $("#addActive").modal('hide');
    })
    $("#btnAddCancel").click(function () {
        $("#addActive").modal("hide");
    })
}

//添加的格式正则
// function inputText() {
//     //录入时间
//     //活动负责人
//     var principal=/^[\u4e00-\u9fa5]{2,6}$/;
//     //活动负责人电话
//     var activeTel=/^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
//     check($("#addPrincipal"),principal);
//     check($("#addActiveTel"),activeTel);
//
//
// }

//添加的格式判断
// function check(input,reg) {
//     input.blur(function () {
//         if(reg.test($(this).val())){
//             $(this).next().text('');
//         }else {
//             $(this).next('格式有误').css('color','red');
//         }
//     })
// }

//删除事件
function addDelEvent() {
    $("#tbActive").on("click",".del",function () {
        // $(this).parent().parent().remove();
        var id=$(this).attr("data-id"); //得到id
        delActiveById(id);
        bindPage();//数组删除完过后，重新生成页码
        displayActiveList();//显示数据
        console.log(schoolActiveArr)

    })
}


//删除 代理
function delActiveById(id) {
    if (sessionStorage.getItem("schoolActiveArr")){
        var arr = sessionStorage.getItem("schoolActiveArr");
        schoolActiveArr = JSON.parse(arr);
    }
    for (var i = 0; i<schoolActiveArr.length; i++) {
        if (schoolActiveArr[i].id == id) {
            setEditInfo(`删除了${schoolActiveArr[i].schoolName}的活动`);
            schoolActiveArr.splice(i, 1);
            break;
        }
    }
    var str = JSON.stringify(schoolActiveArr);
    sessionStorage.setItem("schoolActiveArr",str);
}

//删除点击确定取消
// function del() {
//     if(addDelEvent().value==true){
//         confirm("确定要删除该信息吗？")
//     }else {
//
//     }
// }

//修改活动信息(xxx)
function upDateActive() {
    $("#tbActive").on('click','.upDate',function () {
        if (sessionStorage.getItem("schoolActiveArr")){
            var arr = sessionStorage.getItem("schoolActiveArr");
            schoolActiveArr = JSON.parse(arr);
        }
        //var id = schoolActiveArr[schoolActiveArr.length-1].id;
        //自动获取id，
        var index=$(this).attr("index");
        operid=index;
        $("#addActive").modal('show');
        $("#addStartTime").val(schoolActiveArr[index].startTime);
        $("#addSchool").val(schoolActiveArr[index].schoolName);
        $("#addId").val(schoolActiveArr[index].id);
        $("#addTitle").val(schoolActiveArr[index].title);
        $("#addContent").val(schoolActiveArr[index].content);
        $("#addActiveTime").val(schoolActiveArr[index].activeTime);
        $("#addPrincipal").val(schoolActiveArr[index].principal);
        $("#addActiveTel").val(schoolActiveArr[index].activeTel);
        $("#addState").val(schoolActiveArr[index].State);
    })

}


//记录编辑信息方法
// content:编辑内容  例如：content=`修改了${数组名.name}`的信息;
    function setEditInfo(content) {
        var obj = getSessionData("newNotice");
        var newObj = new Object();
        newObj.editUser = $(".hz_who").text();         //获取用户的账号
        newObj.editContent = content;
        var lt = new Date().toLocaleDateString();
        var newT = lt.split("/");
        var newStr = "";
        for(var i=0;i<newT.length;i++){
            newStr += newT[i]+"-";
        }
        //设置编辑时间
        newObj.editTime =newStr.substr(0,newStr.length-1);
        obj.push(newObj);
        setSessionData("newNotice",obj);
    }

    //得到浏览器中的数据     str:数组名
    function getSessionData(str) {
        if(sessionStorage.getItem(str)) {         //浏览器中存在该数组

        }else {          //不存在，初识化
            if(str == "newNotice"){
                sessionStorage.setItem(str,JSON.stringify(newNotice));
            }
        }
        var obj = JSON.parse(sessionStorage.getItem(str));
        return obj;
    }

    //将数据存储到浏览器      str:数组名  data:数组
    function setSessionData(str,data) {
        var newStr = JSON.stringify(data);
        sessionStorage.setItem(str,newStr);
    }


//显示学校信息列表
function displayActiveList() {

    if (sessionStorage.getItem("schoolActiveArr")){
        var arr = sessionStorage.getItem("schoolActiveArr");
        schoolActiveArr = JSON.parse(arr);
    }
    console.log(schoolActiveArr)
    $("#tbActive").html("");
    $("#checkAll")[0].checked=false;
    var start=(currentPage-1)*pageSize;
    var end=start+pageSize;
    for(var i=start;i<end&&schoolActiveArr.length;i++){
        $("#tbActive").append(`<tr>
                        <td index="${i}" ><input type="checkbox" name=\"checkItem\"></td>
                        <td index="${i}" >${schoolActiveArr[i].id}</td>
                        <td index="${i}" >${schoolActiveArr[i].startTime}</td>
                        <td index="${i}">${schoolActiveArr[i].schoolName}</td>
                        <td index="${i}">${schoolActiveArr[i].title}</td>
                        <td index="${i}">${schoolActiveArr[i].content}</td>
                        <td index="${i}">${schoolActiveArr[i].activeTime}</td>
                        <td index="${i}">${schoolActiveArr[i].principal}</td>
                        <td index="${i}">${schoolActiveArr[i].activeTel}</td>
                        <td index="${i}">${schoolActiveArr[i].State}</td>
                        <td index="${i}"><span class="del" onclick="del()" index="${i}" data-id="${schoolActiveArr[i].id}">删除</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="upDate" index="${i}" data-id="${schoolActiveArr[i].id}">修改</span></td>
                    </tr>`)
    }
}
});

